package com.nc.delivery_project.client.orders;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.ServerFailure;
import com.nc.delivery_project.client.ClientFactory;
import com.nc.delivery_project.client.request.OrderRequest;
import com.nc.delivery_project.shared.proxy.OrderProxy;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Natalie D.
 * Date: 1/17/15
 * Time: 9:18 PM
 */
public class OrdersActivity extends AbstractActivity implements OrdersPresenter{

    private OrdersView view;
    private ClientFactory clientFactory;
    //private OrdersPlace place;
    private String[] tokens;

    public OrdersActivity(OrdersPlace place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
       // this.place=place;
        tokens = place.getToken().split(":");
    }

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        if (view == null) {
            view = new OrdersView();
        }
        view.setPresenter(this);
        panel.setWidget(view.asWidget());
        view.start();
        if (tokens[0].equals("sender"))
        {
            if (tokens[1]!=null)
            {
                loadOrdersBySender(tokens[1]);
            }
        }
        else if (tokens[0].equals("receiver"))
        {
            if (tokens[1]!=null)
                loadOrdersByReceiver(tokens[1]);
        }
        else loadOrders();
    }

    @Override
    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }

    @Override
    public ClientFactory getClientFactory() {
        return clientFactory;
    }

    @Override
    public void loadOrders() {
        final OrderRequest orderRequest = clientFactory.getRequestFactory().createOrderRequest();

        orderRequest.read().with("sender").with("receiver").with("payment").fire(new Receiver<List<OrderProxy>>() {
            @Override
            public void onSuccess(List<OrderProxy> response) {
                view.setDataList(response);
            }

            @Override
            public void onFailure(ServerFailure error) {
                super.onFailure(error);
                GWT.log(error.toString());
            }
        });
    }

    @Override
    public void loadOrdersBySender(String id) {
        final OrderRequest orderRequest = clientFactory.getRequestFactory().createOrderRequest();
        orderRequest.readBySenderId(id).with("sender").with("receiver").with("payment").fire(new Receiver<List<OrderProxy>>() {
            @Override
            public void onSuccess(List<OrderProxy> response) {
                view.setDataList(response);
            }

            @Override
            public void onFailure(ServerFailure error) {
                super.onFailure(error);
                GWT.log(error.toString());
            }
        });
    }

    @Override
    public void loadOrdersByReceiver(String id) {
        final OrderRequest orderRequest = clientFactory.getRequestFactory().createOrderRequest();

        orderRequest.readByReceiverId(id).with("sender").with("receiver").with("payment").fire(new Receiver<List<OrderProxy>>() {
            @Override
            public void onSuccess(List<OrderProxy> response) {
                GWT.log("success");
                view.setDataList(response);
            }

            @Override
            public void onFailure(ServerFailure error) {
                super.onFailure(error);
                GWT.log(error.toString());
            }
        });
    }

}
